package org.web.demo.servlet;


import org.web.demo.common.qo.PageResult;
import org.web.demo.common.qo.UserQueryObject;
import org.web.demo.common.util.StringUtils;
import org.web.demo.dao.IUserDAO;
import org.web.demo.dao.impl.UserDAOImpl;
import org.web.demo.entity.User;
import org.web.demo.service.IUserService;
import org.web.demo.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yexi
 */
@WebServlet("/servlet")
public class UserServlet extends HttpServlet {
    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String cmd = req.getParameter("cmd");
        switch (cmd == null ? "" : cmd) {
            case "delete":
                delete(req, resp);
                break;
            case "change":
                try {
                    change(req, resp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case "saveOrUpdate":
                saveOrUpdate(req, resp);
                break;
            default:
                list(req, resp);
                break;
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserQueryObject qo = new UserQueryObject();

        String currentPage = req.getParameter("currentPage");
        if (StringUtils.hasLength(currentPage)) {
            qo.setCurrentPage(Integer.parseInt(currentPage));
        }
        String pageSize = req.getParameter("pageSize");
        if (StringUtils.hasLength(pageSize)) {
            qo.setPageSize(Integer.parseInt(pageSize));
        }

        String name = req.getParameter("name");
        if (StringUtils.hasLength(name)) {
            qo.setInputString(name);
        }

        String maxAge = req.getParameter("maxAge");
        if (StringUtils.hasLength(maxAge)) {
            qo.setMaxAge(Integer.parseInt(maxAge));
        }

        String minAge = req.getParameter("minAge");
        if (StringUtils.hasLength(minAge)) {
            qo.setMinAge(Integer.parseInt(minAge));
        }

        PageResult pageResult = userService.query(qo);

        req.setAttribute("pageResult", pageResult);
        req.setAttribute("qo", qo);
        req.getRequestDispatcher("WEB-INF/list.jsp").forward(req, resp);
    }

    private void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (StringUtils.hasLength(id)) {
            IUserDAO userDAO = new UserDAOImpl();
            User user = userDAO.selectOne(Long.parseLong(id));
            req.setAttribute("user", user);
        }
        req.getRequestDispatcher("WEB-INF/saveorupdate.jsp").forward(req, resp);
    }

    private void change(HttpServletRequest req, HttpServletResponse resp) throws IOException, ParseException {
        IUserDAO userDAO = new UserDAOImpl();

        String id = req.getParameter("id");
        String u_name = req.getParameter("u_name");
        Integer age = null;
        BigDecimal salary = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

        String u_age = req.getParameter("u_age");
        if (StringUtils.hasLength(u_age)) {
            age = Integer.valueOf(u_age);
        }

        String u_salary = req.getParameter("u_salary");
        if (StringUtils.hasLength(u_salary)) {
            //salary = BigDecimal.valueOf(Double.parseDouble(u_salary));
            salary = new BigDecimal(u_salary);//更优于上面的方法,任何类型都能接收
        }

        String hiredate = req.getParameter("hiredate");
        Date date = simpleDateFormat.parse(hiredate);

        if (StringUtils.hasLength(id)) {
            User user = new User(Long.parseLong(id), u_name, age, salary, date);
            userDAO.update(user);
        } else {
            if ((u_name != null && !u_name.equals("")) && (age != null) && (salary != null)) {
                User user = new User(null, u_name, age, salary, date);
                userDAO.insert(user);
            } else {
                resp.getWriter().write("沙基里的");
                return;
            }
        }
        resp.sendRedirect("/servlet");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        IUserDAO userDAO = new UserDAOImpl();
        if (StringUtils.hasLength(id)) {
            userDAO.delete(Long.parseLong(id));
        }
        resp.sendRedirect("/servlet");
    }
}
